package com.xuecheng.content;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;


/**
 * 责任链设计模式
 */
@Slf4j
public class Zerenlian12 {

    public static void main(String[] args) {
        //1.创建处理器
        Father father = new Father();

        Husband husband = new Husband();

        Son son = new Son();

        //设置顺序
        father.setNext(husband);
        husband.setNext(son);

        //3.创建请求
        Request request = new Request(new Level("2"));

        //4.提交请求
        Response response = father.handleMessage(request);

    }


}


/**
 * 男性处理者
 */
abstract class Handler {
    private Handler nextHandler;


    //核心模版处理方法
    public final Response handleMessage(Request request) {
        Response response = null;
        Level handlerLevel = this.getHandlerLevel();
        if (request.getLevel().equals(this.getHandlerLevel())) {
            return this.echo(request);
        }

        //2.当前存在后续处理者，则传递给下一个处理者
        else if (this.nextHandler != null) {
            return this.nextHandler.handleMessage(request);
        } else {
            //自定义处理逻辑
        }
        return response;
    }


    public void setNext(Handler handler) {
        this.nextHandler = handler;
    }

    //抽象方法，获取处理级别
    protected abstract Level getHandlerLevel();

    //处理请求
    protected abstract Response echo(Request request);

}

/**
 * 父亲处理者
 */
class Father extends Handler {
    @Override
    protected Level getHandlerLevel() {
        return new Level("1");
    }

    @Override
    protected Response echo(Request request) {
        System.out.println("父亲处理");
        return new Response("父亲处理");
    }
}


/**
 * 丈夫处理者
 */
class Husband extends Handler {
    @Override
    protected Level getHandlerLevel() {
        return new Level("2");
    }

    @Override
    protected Response echo(Request request) {
        System.out.println("丈夫处理");
        return new Response("丈夫处理");
    }
}


/**
 * 儿子处理者
 */
class Son extends Handler {
    @Override
    protected Level getHandlerLevel() {
        return new Level("3");
    }

    @Override
    protected Response echo(Request request) {
        System.out.println("儿子处理");
        return new Response("儿子处理");
    }
}


@Data
class Request {
    private Level Level;

    public Request(Level level) {
        Level = level;
    }
}

@Data
class Response {
    private String msg;

    public Response(String msg) {
        this.msg = msg;
    }
}

@Data
class Level {
    private String level;

    public Level(String level) {
        this.level = level;
    }
}


//第二种方式处理
class  womenClient{
    public static void main(String[] args) {
        //母亲想要出去逛街
        //1.父亲审核
        Fathers fathers = new Fathers();
        //2.丈夫审核
        Hub hub = new Hub();
        //3.儿子审核
        Sons son = new Sons();

        IWomen fWomen = new IWomen(1,"我要出去逛街");
        String s1 = fathers.handleMessage(fWomen);
        System.out.println(s1);
        IWomen sWomen = new IWomen(2,"给儿子做饭");
        String s2 = son.handleMessage(sWomen);
        System.out.println(s2);
        IWomen hWomen = new IWomen(2,"给丈夫洗衣服");
        String s3 = hub.handleMessage(hWomen);
        System.out.println(s3);
    }
}




interface Women {
    /**
     * 当前处于的状态，1:在家从父，2:出家从夫，3:父死从子
     */
    Integer getType();

    /**
     * 1: 逛街
     * 2: 吃饭
     * 3: 睡觉
     */
    String getRequest();
}

/***
 * 母亲
 */
class IWomen implements Women {
    //当前状态
    private Integer type;

    //要做的操作
    private   String operation;

    public IWomen(Integer type, String operation) {
        this.type = type;
        this.operation = operation;
    }

    @Override
    public Integer getType() {
        return this.type;
    }

    @Override
    public String getRequest() {
        return operation;
    }
}


interface HandlerWomen {
    //处理母亲的请求
    abstract String handleMessage(Women women);
}


class Fathers implements HandlerWomen {

    @Override
    public String handleMessage(Women women) {
        return String.join("母亲向父亲请示：%", women.getRequest());
    }
}



class Hub implements HandlerWomen {

    @Override
    public String handleMessage(Women women) {
        return String.join("母亲向丈夫请示：%", women.getRequest());
    }
}


class Sons implements HandlerWomen {

    @Override
    public String handleMessage(Women women) {
        return String.join("母亲向儿子请示：%", women.getRequest());
    }
}